/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Form;

import BLL.BLLImport;
import DAL.ConnectionDB;
import DTO.DTOImport;
import DTO.DTOStatistic;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Pham
 */
public class JPanelStatisticImport extends javax.swing.JPanel {
    BLL.BLLImport bllImp;
    /**
     * Creates new form JPanelStatisticImport
     */
    public JPanelStatisticImport() {
        bllImp = new BLLImport();
        initComponents();
        loadHistoryImport();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        Month = new com.toedter.calendar.JMonthChooser();
        Year = new com.toedter.calendar.JYearChooser();
        btnStatistic = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbViewImport = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        lbSum = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lbSumMoney = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        btnPrintStatistic = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbHistory = new javax.swing.JTable();

        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setPreferredSize(new java.awt.Dimension(923, 520));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thống kê số lượng nhập trong tháng", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(204, 0, 0)));

        jLabel2.setText("Chọn thời gian ");

        Month.setNextFocusableComponent(Year);

        Year.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        Year.setNextFocusableComponent(btnStatistic);

        btnStatistic.setBackground(new java.awt.Color(255, 153, 153));
        btnStatistic.setText("Thống kê");
        btnStatistic.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStatisticActionPerformed(evt);
            }
        });

        jScrollPane1.setPreferredSize(new java.awt.Dimension(394, 402));

        tbViewImport.setAutoCreateRowSorter(true);
        tbViewImport.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tbViewImport);

        jLabel3.setText("Tổng số lượng sản phẩm nhập trong tháng:");

        lbSum.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        lbSum.setForeground(new java.awt.Color(0, 0, 102));

        jLabel4.setText("Tổng số tiền đã chi trong tháng: ");

        lbSumMoney.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        lbSumMoney.setForeground(new java.awt.Color(204, 0, 51));

        jLabel5.setText("sản phẩm");

        jLabel6.setText("VNĐ");

        btnPrintStatistic.setText("In thống kê");
        btnPrintStatistic.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrintStatisticActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lbSum, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lbSumMoney, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(Month, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(Year, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnStatistic, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnPrintStatistic)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel2)
                    .addComponent(Month, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Year, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnStatistic, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(lbSum, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(lbSumMoney, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnPrintStatistic)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Lịch sử nhập hàng", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 153)));

        tbHistory.setAutoCreateRowSorter(true);
        tbHistory.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(tbHistory);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 439, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(126, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnStatisticActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStatisticActionPerformed
        try{
            DefaultTableModel tbModel = new DefaultTableModel();
            String[] cols = {"Tên sản phẩm", "Số lượng đã nhập", "Thành tiền (VNĐ)"};
            tbModel.setColumnIdentifiers(cols);
            
            String month = String.valueOf(Month.getMonth() + 1);
            String year =  String.valueOf(Year.getYear());
            int sum = 0;
            long sumMoney = 0;
            for(DTOStatistic itemSt : bllImp.statisticImport(month, year)){
                long resultMoney = itemSt.getPrice() * itemSt.getQuantity();
                tbModel.addRow(new Object[]{itemSt.getProName(), itemSt.getQuantity(), resultMoney});
                sum += itemSt.getQuantity();
                sumMoney += resultMoney;
            }
            tbViewImport.setModel(tbModel);
            tbViewImport.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
            lbSum.setText(String.valueOf(sum));
            lbSumMoney.setText(String.valueOf(sumMoney));
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Không thể thống kê");
        }
    }//GEN-LAST:event_btnStatisticActionPerformed

    private void btnPrintStatisticActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrintStatisticActionPerformed
        try{
            String month = String.valueOf(Month.getMonth() + 1);
            String year =  String.valueOf(Year.getYear());
            String query = "SELECT " +
                    "     product.`product_id` AS product_product_id, " +
                    "     product.`product_name` AS product_product_name, " +
                    "     manufacture.`manufacture_name` AS manufacture_manufacture_name, " +
                    "     SUM(import.`import_quantity`) AS import_import_quantity, " +
                    "     import.`import_price` AS import_import_price " +
                    "FROM `product` product, `manufacture` manufacture, `import` import " +
                    "WHERE product.`product_id` = import.`product_id` AND manufacture.`manufacture_id` = product.`manufacture_id` " +
                    " AND MONTH(import.`import_date`) like '" + month + "' AND YEAR(import.`import_date`) like '" + year + "' " +
                    "GROUP BY " +
                    "product.`product_id`";
            String source = getClass().getResource("/Report/ReportStatisticImport.jrxml").toString();
            source = source.substring(6, source.length());
            JasperReport jr = JasperCompileManager.compileReport(source);
            ConnectionDB conn = new ConnectionDB();
            PreparedStatement pstmt = conn.getConnectionDB().prepareStatement(query);
            ResultSet rs = pstmt.executeQuery();
            JRResultSetDataSource datasource = new JRResultSetDataSource(rs);
            JasperPrint jp = JasperFillManager.fillReport(jr, null, datasource);
            if(jr == null)
                JOptionPane.showMessageDialog(null, "không có sản phẩm nào được nhập vào tháng " + month + " năm " + year);
            JasperViewer jv = new JasperViewer(jp, false);
            jv.setVisible(true);
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Loi, khong the in bao cao");
        }
    }//GEN-LAST:event_btnPrintStatisticActionPerformed

    private void loadHistoryImport()
    {
        try{
            String[] cols = {"Ngày nhập", "Tên sản phẩm", "Số lượng đã nhập", "Giá nhập"};
            DefaultTableModel tbModel = new DefaultTableModel();
            tbModel.setColumnIdentifiers(cols);
            for(DTOImport dtoImp : bllImp.getImports()){
                tbModel.addRow(new Object[]{dtoImp.getImportDate(), dtoImp.getDtoPro().getProductName(), dtoImp.getImportQuantity(), dtoImp.getImportPrice()});
            }
            tbHistory.setModel(tbModel);
            tbHistory.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        }
        catch(Exception ex){
            JOptionPane.showMessageDialog(null, "Loi, khong the tai du lieu");
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.toedter.calendar.JMonthChooser Month;
    private com.toedter.calendar.JYearChooser Year;
    private javax.swing.JButton btnPrintStatistic;
    private javax.swing.JButton btnStatistic;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lbSum;
    private javax.swing.JLabel lbSumMoney;
    private javax.swing.JTable tbHistory;
    private javax.swing.JTable tbViewImport;
    // End of variables declaration//GEN-END:variables
}
